package PageTable;

import page.Page;
import utils.JdbcUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.List;

public class PageTable extends Box {
    public PageTable(int power,String name){
        super(BoxLayout.Y_AXIS);
        //创建按钮
        JPanel btnPanel =new JPanel();
        btnPanel.setMaximumSize(new Dimension(800,20));
        JButton addBtn=new JButton("添加");
        JButton updateBtn=new JButton("修改");
        JButton deleteBtn=new JButton("删除");
        JButton queryBtn=new JButton("查询");
        btnPanel.add(addBtn);
        btnPanel.add(updateBtn);
        btnPanel.add(deleteBtn);
        btnPanel.add(queryBtn);
        this.add(btnPanel);
        addBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddPage.add(power,name);
            }
        });
        updateBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdataPage.updata(power,name);
            }
        });
        deleteBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DeletePage.delete();
            }
        });
        queryBtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                QueryPage.find(power, name);
            }
        });
        String[] ts={"文章编号","文章名","作者","文章分类","文章内容","文章创建时间","最后一次更改时间","文章权限"};
        String[][] begin =data_update();
        JTable table =new JTable();
        table.setEnabled(false);
        DefaultTableModel model =new DefaultTableModel(begin,ts); // 新建一个默认数据模型
        JScrollPane scrollPane=new JScrollPane(table);
        table.setModel(model);
        add(scrollPane);
    }
    public String[][] data_update(){
        int MX= Page.sum;
        String[][] data =new String[MX][8];
        int sum=0;
        List<Map<String, Object>> datas= JdbcUtils.JdbcQuery("SELECT * FROM page");
        for(Map<String,Object> map : datas){
            int id = 0;
            String name = null;
            String class_ = null;
            String content = null;
            LocalDateTime betime = null;
            LocalDateTime updatetime = null;
            int power = 0;
            String username = null;
            int comment_sum=0;
            for (String key : map.keySet()) {
                if (key.equals("page_id")) id = (int) map.get(key);
                if (key.equals("page_name")) name = (String) map.get(key);
                if (key.equals("page_class")) class_ = (String) map.get(key);
                if (key.equals("page_content")) content = (String) map.get(key);
                if (key.equals("page_releasedata")) {
                    Object value = map.get(key);
                    if (value instanceof Timestamp) {
                        betime = ((Timestamp) value).toLocalDateTime();
                    } else if (value instanceof LocalDateTime) {
                        betime = (LocalDateTime) value;
                    }
                }
                if (key.equals("page_update")) {
                    Object value = map.get(key);
                    if (value instanceof Timestamp) {
                        updatetime = ((Timestamp) value).toLocalDateTime();
                    } else if (value instanceof LocalDateTime) {
                        updatetime = (LocalDateTime) value;
                    }
                }
                if (key.equals("page_permissions")) power = (int) map.get(key);
                if (key.equals("page_user")) username = (String) map.get(key);
            }
            data[sum][0]= String.valueOf(id);data[sum][2]= String.valueOf(username);
            data[sum][1]= String.valueOf(name);data[sum][3]= String.valueOf(class_);
            data[sum][4]= String.valueOf(content);data[sum][5]= String.valueOf(betime);
            data[sum][6]= String.valueOf(updatetime);data[sum][7]= String.valueOf(power);
            sum++;
        }
        return data;
    }
}
